<% admin_breadcrumb(link_to Spree.t(:stock), spree.admin_stock_items_path) %>
<% admin_breadcrumb(plural_resource_name(Spree::StockTransfer)) %>


<% content_for :page_actions do %>
  <% if can? :create, Spree::StockTransfer %>
    <li>
      <%= button_link_to Spree.t(:new_stock_transfer), new_admin_stock_transfer_path %>
    </li>
  <% end %>
<% end %>

<% content_for :table_filter_title do %>
  <%= Spree.t(:search) %>
<% end %>

<% content_for :table_filter do %>
  <div data-hook="admin_stock_transfers_index_search">
    <%= search_form_for [:admin, @search] do |f| %>
      <div class="row">
        <div class="field-block col-xs-3">
          <div class="field">
            <%= f.label nil, Spree::StockLocation.model_name.human %>
            <%= f.select :source_location_id_or_destination_location_id_eq, options_from_collection_for_select(@stock_locations, :id, :name, params[:q][:source_location_id_or_destination_location_id_eq]), {include_blank: true}, {class: 'select2 fullwidth'} %>
          </div>
        </div>

        <div class="field-block col-xs-3">
          <div class="date-range-filter field">
            <%= f.label nil, Spree.t(:date_range) %>
            <div class="date-range-fields">
              <%= f.text_field :created_at_gt, class: 'datepicker datepicker-from', include_blank: true, value: params[:q][:created_at_gt], placeholder: Spree.t(:start) %>

              <span class="range-divider">
                <i class="fa fa-arrow-right"></i>
              </span>

              <%= f.text_field :created_at_lt, class: 'datepicker datepicker-to', include_blank: true, value: params[:q][:created_at_lt], placeholder: Spree.t(:stop) %>
            </div>
          </div>
        </div>

        <div class="field-block col-xs-3">
          <div class="field">
            <%= f.label nil, Spree::StockTransfer.human_attribute_name(:number) %>
            <%= f.text_field :number_cont, value: params[:q][:number_cont] %>
          </div>
        </div>

        <div class="field-block col-xs-3">
          <div class="field checkbox">
            <label>
              <%= f.check_box :closed_at_null, { checked: @show_only_open }, '1', '0' %>
              <%= Spree.t(:show_only_open_transfers) %>
            </label>
          </div>
        </div>
      </div>

      <div class="clearfix"></div>

      <div class="actions filter-actions">
        <div data-hook="admin_stock_transfers_index_search_buttons">
          <%= button Spree.t(:filter_results) %>
        </div>
      </div>
    <% end %>
  </div>
<% end %>

<%= paginate @stock_transfers, theme: "solidus_admin" %>


<% if @stock_transfers.any? %>
  <table class="index" id="listing_stock_transfers">
    <thead>
      <tr>
        <th><%= sort_link @search, :number,          Spree.t(:number) %></th>
        <th><%= sort_link @search, :from,            Spree.t(:from) %></th>
        <th><%= sort_link @search, :to,              Spree.t(:to) %></th>
        <th><%= sort_link @search, :expected_items,  Spree.t(:expected) %></th>
        <th><%= sort_link @search, :received_items,  Spree.t(:received) %></th>
        <th><%= sort_link @search, :shipped_at,      Spree.t(:shipped) %></th>
        <th><%= sort_link @search, :status,          Spree.t(:status) %></th>
        <th class="actions"></th>
      </tr>
    </thead>
    <tbody>
      <% @stock_transfers.each do |stock_transfer| %>
        <tr id="<%= spree_dom_id stock_transfer %>" class="<%= cycle('odd', 'even') %>">
          <td><%= handle_stock_transfer(stock_transfer) %></td>
          <td><%= stock_transfer.source_location.name %></td>
          <td><%= stock_transfer.destination_location.try(:name) %></td>
          <td class="align-center"><%= stock_transfer.expected_item_count %></td>
          <td class="align-center"><%= stock_transfer.received_item_count %></td>
          <td><%= stock_transfer.shipped_at.try(:to_date) %></td>
          <td><%= stock_transfer_status(stock_transfer) %></td>
          <td class="actions">
            <% if stock_transfer.receivable? && can?(:edit, stock_transfer) %>
              <%= link_to_with_icon 'download', Spree.t('actions.receive'), receive_admin_stock_transfer_path(stock_transfer), no_text: true, data: { action: 'green' } %>
            <% elsif !stock_transfer.closed? && can?(:edit, stock_transfer) %>
              <%= link_to_with_icon 'edit', Spree.t('actions.edit'), stock_transfer_edit_or_ship_path(stock_transfer), no_text: true, data: { action: 'edit' } %>
            <% elsif can?(:show, stock_transfer) %>
              <%= link_to_with_icon 'eye', Spree.t(:show), admin_stock_transfer_path(stock_transfer), no_text: true, data: { action: 'green' } %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
<% else %>
  <div class="col-xs-9 no-objects-found">
    <%= render 'spree/admin/shared/no_objects_found',
                  resource: Spree::StockTransfer,
                  new_resource_url: new_object_url %>
  </div>
<% end %>

<%= paginate @stock_transfers, theme: "solidus_admin" %>
